﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SunwellRenderer.Simulation;
using SunwellRenderer.Rendering;

using Librarinth;

namespace SunwellRenderer
{
	public class SunwellMain : Game
	{
		static SunwellMain instance;
		public static SunwellMain Instance { get { return instance; } }

		public readonly RenderingModule Renderer;
		public readonly SimulationCore Simulation;
		public readonly SpatialModule SpatialModule;
		public SunwellMain()
		{
			if(instance != null)
				throw new Exception("Cannot duplicate Singleton");
			instance	= this;

			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
			FPS = new FPScounter();

			graphics.PreferredBackBufferWidth = 800;
			graphics.PreferredBackBufferHeight = 500;
			graphics.IsFullScreen = false;

			SpatialModule	= new SpatialModule();
			Renderer	= new RenderingModule();
			Simulation	= new SimulationCore();
        }

		protected override void Initialize()
		{
			graphics.SynchronizeWithVerticalRetrace = false;
			IsFixedTimeStep = false;
			base.Initialize();
		}

		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;
		SpriteFont font;
		FPScounter FPS;
		protected override void LoadContent()
		{
			spriteBatch	= new SpriteBatch(GraphicsDevice);
			font	= Content.Load<SpriteFont>("font");

			Renderer.LoadContent();
			Simulation.LoadContent();
		}

		protected override void Update(GameTime gameTime)
		{
			FPS.Update(gameTime);

			Simulation.Update(gameTime);

			base.Update(gameTime);
		}

		protected override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);

			FPS.DrawCheck();

			Renderer.Draw();

			spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.Immediate,SaveStateMode.None);
			spriteBatch.DrawString(font,"FPS " + FPS.FrameRate + "\nMesh counter " + Renderer.MeshCounter,Vector2.Zero,Color.Red);
			spriteBatch.End();
		}

		////////////////////////////////////////////////////////////////////////////////

		static void Main(string[] args)
		{
			using(var game = new SunwellMain())
				Instance.Run();
		}
    }
}
